<?php
	session_start();
        include_once(dirname(__FILE__). "/../../core/managers/business/class.user.php");
	include_once(dirname(__FILE__). "/../../core/presenters/administration/orderAdministration/class.orderAdministration_Presenter.php");

        $user_connected = unserialize($_SESSION["current_user"]);
        $pres = new OrderAdministration_Presenter($user_connected->getId());

        if(((!isset($user_connected) || $user_connected == "")|| $user_connected->getAdmin() == 0) && (! $pres->getIfUserIsOrderAdmin($user_connected->getId()))){
            //header('index.php');
            echo "<script language='JavaScript'> window.location = '../../home.php'; </script>";
        }else{
            $_SESSION['ord_adm_pres'] = serialize($pres);
        }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
	<head>
  		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  		<link rel="stylesheet" media="screen" href="../../generic/generic.css" />
  		<link rel="stylesheet" media="screen" href="admin_order.css" />
		<link rel="icon" type="image/png" href="../../content/images/favicon.ico" />
		
		<!--JQUERY-->
		<script language='JavaScript' src='../../scripts/jquery-ui-1.8.6.custom/js/jquery-1.4.4.min.js'></script>
		<script language='JavaScript' src='../../scripts/jquery-ui-1.8.6.custom/js/jquery-ui-1.8.6.custom.min.js'></script>
		<link rel="stylesheet" media="screen" type="text/css" title="style" href="../../scripts/jquery-ui-1.8.6.custom/css/ui-darkness/jquery-ui-1.8.6.custom.css" />	
		
		<!--Autres scripts-->
		<script language='JavaScript' src='../../scripts/generic.js'></script>
		<script language='JavaScript' src='../../scripts/menu_manager.js'></script>
		<script language='JavaScript' src='admin_order.js'></script>
  		<title>
			Identificator - Administration des ordres
		</title>
	</head>
	<body onload="Hour();">
                <img id="image_fond" src="../../content/images/background/fond.png" style="width:99%; height:99%;"/>
		<?php
			include_once(dirname(__FILE__). "/../../generic/top_level2.php");
			
                        $user_picture_dir = "../../".$pres->getUserPictureDirectory();
                        
			if($user_connected->getPhotoProfil() != ""){
                            echo "<input type='hidden' id='photo_profil' value='".$user_picture_dir.$user_connected->getPhotoProfil()."' />";
                        }
                        else echo "<input type='hidden' id='photo_profil' value='../../content/images/Default_white.png' />";
		?>
		<script language='JavaScript'>
			InitHeader(document.getElementById('photo_profil').value, "Administration des ordres ...", '... voici la liste des ordres', true);
		</script>
		 <div id="content">
			<div id="menu" class="menu">
				<img id="menuImg1" class="menuImg" src="../../content/images/menus/fleche_menu.png" onmouseover="javascript:itemMenuOver(this)" 
                                                                                                                    onmouseout="javascript:itemMenuOver(this)" 
                                                                                                                    style="cursor:pointer;"
                                                                                                                    onclick="javascript:window.location='../../menu.php'"/>
			
				<img id="menuImg2" class="menuImg" src="../../content/images/menus/fleche_admin.png" onmouseover="javascript:itemMenuOver(this)" 
                                                                                                                        onmouseout="javascript:itemMenuOver(this)" 
                                                                                                                        style="cursor:pointer;"
                                                                                                                        onclick="javascript:window.location='../administration.php'"/>
			
				<img id="menuImg3" class="menuImg" src="../../content/images/menus/fleche_ordres.png" onmouseover="javascript:itemMenuOver(this)" 
                                                                                                                        onmouseout="javascript:itemMenuOver(this)" 
                                                                                                                        style="cursor:pointer;"
                                                                                                                        onclick="javascript:window.location='admin_order.php'"/>
			</div>
			<div id="div_middle" class="div_middle">
				<span id='label_Info' class='label_Info'>
                                    
				</span>
                                 <?php
                                    $users  = $pres->getAllUsers();
                                    $orderList = $pres->getAllAuthorizedOrder($user_connected->getId());
                                    foreach($orderList as $order){
                                        
                                        //--Gestion de l'image---------------------------------------------------
                                        $result = false;
                                        $message="";
                                        
                                        $content_dir = "../../".$pres->getDirTemp();
                                        //$user_picture_dir = "../../".$pres->getUserPictureDirectory();

                                        if( isset($_POST["UploadButton_".$order->getId()]) ) // si formulaire soumis
                                        {
                                            $result = true;
                                            $selected = $order->getId();

                                            $tmp_file = $_FILES["FileUploadControl_".$order->getId()]['tmp_name'];//--Nom du fichier temporaire

                                            if( !is_uploaded_file($tmp_file) ) //--SI le fichier à bien été chargé
                                            {
                                                $message ="Le fichier est introuvable";
                                                $result = false;
                                            }

                                            $name_file = $_FILES["FileUploadControl_".$order->getId()]['name'];// on copie le fichier dans le dossier de destination

                                            if( preg_match('#[\x00-\x1F\x7F-\x9F/\\\\]#', $name_file) )//vérification hacking
                                            {
                                                $message ="Nom de fichier non valide";
                                            }
                                            else if( !move_uploaded_file($tmp_file, $content_dir . $name_file) && $result ) //--SI le fichier à bien été uploadé
                                            {
                                                $message="Impossible de copier le fichier dans $content_dir";
                                                $result = false;
                                            }

                                            if($result){
                                                $message ="Le fichier est chargé";
                                            }
                                        }
                                        else $message= "Sélectionnez un fichier: taille < 2Mo";
                                        //------------------------------------------------------------------------------------------
                                        //echo "<A HREF='#ancre_".$order->getId()."'>";
                                        echo "<form method='post' enctype='multipart/form-data' action='admin_order.php'>";
                                        if($order->getId() == 0){
                                            echo "<div class='order' id='order_".$order->getId()."' style='display:none;'>";
                                        }
                                        else{
                                            echo "<div class='order' id='order_".$order->getId()."'>";
                                        }
                                             echo "<div class='photo_order'>";
                                                    if($result){
                                                            echo "<img src='".$content_dir.$_FILES["FileUploadControl_".$order->getId()]['name']."' />";
                                                            echo "<input type='hidden' id='hiddenPhoto_".$order->getId()."' value='".$_FILES["FileUploadControl_".$order->getId()]['name']."' />";
							}
                                                        else if($pres->getOrderPictureDirectory()."/".$order->getLienImage() != "" ){
                                                            echo "<img src='../../".$pres->getOrderPictureDirectory()."/".$order->getLienImage()."' />";
                                                            echo "<input type='hidden' id='hiddenPhoto_".$order->getId()."' value='".$order->getLienImage()."' />";
                                                        }
                                                        
                                                    echo "</div>
                                                    <div class='infos_order'>";
                                                        if(isset($_POST["nom_".$order->getId()]) ){
                                                            echo "<span>Nom : <input type='text' name='nom_".$order->getId()."' id='nom_".$order->getId()."' value='".$_POST["nom_".$order->getId()]."' /></span>";  
                                                        }//--Sinon rechercher dans la base
                                                        else {
                                                            echo "<span>Nom : <input type='text' name='nom_".$order->getId()."' id='nom_".$order->getId()."' value='".$order->getNom()."' /></span>";
                                                        }
                                                    echo "
                                                        <a href='#modal_".$order->getId()."' name='modal'>
                                                            <img class='man_adm_pic' src='../../content/images/comptes_utilisateurs.png' />   
                                                        </a> 
                                                    </div>
                                                    <div class='traitement_order'>
                                                        <div class='zone_image'>";
                                            echo $message;
                                               echo"        <input type='file' name='FileUploadControl_".$order->getId()."' id='FileUploadControl_".$order->getId()."' accept='image/*'/>
                                                            <input type='submit' name='UploadButton_".$order->getId()."' id='charger_".$order->getId()."' Value='Charger' class='bouton bout_charger' /> 
                                                        </div>
                                                        <div class='zone_action'>";
                                                        if($order->getId() == 0){
                                                            echo "<input type='button' id='add_".$order->getId()."' onclick=\"add()\" Value='Ajouter' class='bouton bout_add' /> ";
                                                        }
                                                        else{
                                                            echo "<input type='button' id='mod_".$order->getId()."' onclick=\"update(".$order->getId().", '".$order->getNom()."')\" Value='Modifier' class='bouton bout_sup' /> ";
                                                        }
                                                        if($pres->orderIsDeletable($order->getId())){
                                                            echo "<input type='button' id='sup_".$order->getId()."' onclick=\"del(".$order->getId().", '".$order->getNom()."')\" Value='Supprimer' class='bouton bout_mod' /> ";     
                                                        }
                                                        else{
                                                            echo "<input type='button' id='sup_".$order->getId()."' onclick=\"del_forbidden('".$order->getNom()."')\" Value='Supprimer' class='bouton bout_mod' /> ";     
                                                        }
                                                echo "</div>
                                                    </div>
                                                </div>
                                              </form>";
                                        //--Gestion de la modale popup
                                        echo "<div class='modal_popup' id='modal_".$order->getId()."'>";
                                        if($order->getId() != 0){
                                            echo "
                                                    Cliquez-glissez de gauche à droite pour les définir les administrateurs de l'ordre : ".$order->getNom()." 
                                                <div id='bloc_gauche'>
                                                    <div class='titre_div1'>Personnes</div>
                                                    <div id='conteneur_user' class='personnes_gauche droppable_user'>
                                                        ";
                                            //$admins = null;
                                            //$users = null;
                                            $admins = $pres->getAdminToOrder($order->getId());
                                            $nonAdmins = null;
                                            //--Gestion de la liste des utilisateur
                                            foreach($users as $user){
                                                //-Si la liste des admins n'est pas vide
                                                $est_admin = false;
                                                if(count($admins) > 0){
                                                    foreach($admins as $admin){
                                                        if($admin != "badLog"){
                                                            //--Vérifie que l'utilisateur ne soit pas déja administrateur de cet ordre
                                                            if($admin->getId() == $user->getId()){
                                                                $est_admin = true;
                                                            }
                                                        }
                                                    }
                                                }/*else{
                                                    $nonAdmins[] = $user;
                                                }*/
                                                if(!$est_admin){
                                                    $nonAdmins[] = $user;
                                                }
                                            }

                                            foreach($nonAdmins as $nonAdmin){
                                                echo "<div class='div_user draggable_user' id='order_".$order->getId()."_nonadmin_".$nonAdmin->getId()."'>
                                                        ".$nonAdmin->getPrenom()." ".$nonAdmin->getNom()."
                                                    </div>";
                                            }

                                            //--Gestion de la liste des administrateur actuels
                                            echo "</div></div>
                                                <div id='bloc_droit'>
                                                <div class='titre_div2'>Administrateurs</div>
                                                    <div id='conteneur_admin' class='personnes_droite droppable_admin'>";
                                                    if(count($admins) > 0){
                                                        foreach($admins as $admin){
                                                            if($admin != "badLog"){
                                                                echo "<div class='div_user draggable_admin' id='order_".$order->getId()."_admin_".$admin->getId()."'>
                                                                    ".$admin->getPrenom()." ".$admin->getNom()." ";     
                                                                echo "</div>";
                                                            }
                                                        }   
                                                    }
                                            echo "</div> 
                                                </div>
                                                <span class='text_info_admins' style='color:green;'></span>";
                                        }
                                        else{
                                            echo "Vous devez enregistrer l'ordre avant de définir les administrateur.";
                                        }
                                        echo "</div>";
                                    }
                                ?>
				<!--<input type='button' id='ajouter_ordre' onclick="add();" value="Ajouter ordre" class='bouton bout_add' /> -->
                            <?php 
                            if($user_connected->getAdmin() == 1){ 
                                echo "<div id='div_add_ordre' class='div_add_ordre'  >
                                        <img id='img_add_ordre' class='img_add_ordre' src='../../content/images/ajouter-orange.png' onclick='add_new_order();' />
                                    </div> ";
                             } 
                             else{
                                 echo "<div id='div_add_ordre' class='div_add_ordre' >
                                        <img id='img_add_ordre' class='img_add_ordre' src='../../content/images/ajouter-orange.png' onclick='add_new_order();' style='display:none'  />
                                    </div> ";
                             }
                             ?>
			</div>
                        <span id="label_error" class="label_error"></span>
		</div>
                <?php
                    //--si un item à été cliqué au dernier coup
                     if(isset($selected)){
                        if($selected == 0){
                            $plusclique = true;
                            echo "<script>javascript:new_order_visble();</script>";
                            echo "<script>javascript:positionScrollEnBas();</script>";
                        }
                        else{
                            echo "<script>location.href='#order_".$selected."'</script>";
                        }
                    }
                    //--Si le nouvel item à déja été cliqué
                    if($plusclique == true){
                        echo "<script>javascript:new_order_visble();</script>";
                    }
                ?>
		<?php
			include_once(dirname(__FILE__). "/../../generic/footer_level2.php");
		?>
              <div id="mask"></div>
	</body>
</html>
